<?php
/* [$WindsForce] (C)Hunzhiwange.COM Since 2014.07.01.
   家居公司基本表模型($$)*/

!defined('Q_PATH') && exit;

class JiajucompanyModel extends CommonModel{

	static public function init__(){
		return array(
			'table_name'=>'jiajucompany',
			'autofill'=>array(
				array('user_id','userId','create','callback'),
				array('jiajucompany_username','userName','create','callback'),
				array('jiajucompany_edituserid','userId','create','callback'),
				array('jiajucompany_editusername','userName','create','callback'),
			),
			'check'=>array(
				'jiajucompany_fullname'=>array(
					array('require',Q::L('公司全称不能为空','__APPJIAJU_COMMON_LANG__@Model')),
					array('max_length',150,Q::L('公司全称不能超过150个字符','__APPJIAJU_COMMON_LANG__@Model')),
				),
				'jiajucompany_shortname'=>array(
					array('require',Q::L('公司简称不能为空','__APPJIAJU_COMMON_LANG__@Model')),
					array('max_length',100,Q::L('公司简称不能超过100个字符','__APPJIAJU_COMMON_LANG__@Model')),
				),
				'jiajucompany_address'=>array(
					array('max_length',250,Q::L('公司的详细地址不能超过250个字符','__APPJIAJU_COMMON_LANG__@Model')),
				),
				'jiajucompany_worktime'=>array(
					array('require',Q::L('公司的营业时间不能为空','__APPJIAJU_COMMON_LANG__@Model')),
					array('max_length',250,Q::L('公司的营业时间不能超过250个字符','__APPJIAJU_COMMON_LANG__@Model')),
				),
				'jiajucompany_description'=>array(
					array('max_length',100,Q::L('商家美化描述不能超过100个字符','__APPJIAJU_COMMON_LANG__@Model')),
				),
				'jiajucompany_contactname'=>array(
					//array('require',Q::L('公司联系人不能为空','__APPJIAJU_COMMON_LANG__@Model')),
					array('max_length',20,Q::L('公司联系人不能超过20个字符','__APPJIAJU_COMMON_LANG__@Model')),
				),
				'jiajucompany_telephone'=>array(
					//array('require',Q::L('公司联系电话不能为空','__APPJIAJU_COMMON_LANG__@Model')),
					array('max_length',150,Q::L('公司联系电话不能超过150个字符','__APPJIAJU_COMMON_LANG__@Model')),
				),
				'jiajucompany_qq'=>array(
					array('max_length',40,Q::L('公司联系QQ不能超过40个字符','__APPJIAJU_COMMON_LANG__@Model')),
				),
				'jiajucompany_site'=>array(
					array('empty'),
					array('url',Q::L('公司的网站网址格式错误','__APPJIAJU_COMMON_LANG__@Model')),
					array('max_length',150,Q::L('公司的网站网址不能超过150个字符','__APPJIAJU_COMMON_LANG__@Model')),
				),
				'jiajucompany_domain'=>array(
					array('max_length',100,Q::L('公司的VIP域名不能超过100个字符','__APPJIAJU_COMMON_LANG__@Model')),
				),
				'jiajucompany_email'=>array(
					array('empty'),
					array('email',Q::L('商家邮箱格式不正确','__APPJIAJU_COMMON_LANG__@Model')),
				),
				'jiajucompany_fenji'=>array(
					array('max_length',50,Q::L('商家400分机不能超过50个字符','__APPJIAJU_COMMON_LANG__@Model')),
				),
				'jiajucompany_logo'=>array(
					array('max_length',100,Q::L('公司LOGO不能超过100个字符','__APPJIAJU_COMMON_LANG__@Model')),
				),
				'jiajucompany_baidumap'=>array(
					array('max_length',100,Q::L('公司地图标注点不能超过100个字符','__APPJIAJU_COMMON_LANG__@Model')),
				),
				'jiajucompany_jiaotong'=>array(
					array('max_length',250,Q::L('公司交通方式不能超过250个字符','__APPJIAJU_COMMON_LANG__@Model')),
				),
				'jiajucompany_foundingyear'=>array(
					array('max_length',4,Q::L('公司成立时间不能超过4个字符','__APPJIAJU_COMMON_LANG__@Model')),
				),
				'jiajucompany_scale'=>array(
					array('max_length',200,Q::L('公司规模不能超过200个字符','__APPJIAJU_COMMON_LANG__@Model')),
				),
				'jiajucompany_afterservices'=>array(
					array('max_length',200,Q::L('装后服务不能超过200个字符','__APPJIAJU_COMMON_LANG__@Model')),
				),
				'jiajucompany_earlydesign'=>array(
					array('max_length',200,Q::L('初期设计/量房不能超过200个字符','__APPJIAJU_COMMON_LANG__@Model')),
				),
				'jiajucompany_initialbudget'=>array(
					array('max_length',200,Q::L('初期预算/洽谈方式不能超过200个字符','__APPJIAJU_COMMON_LANG__@Model')),
				),
				'jiajucompany_designeddeep'=>array(
					array('max_length',200,Q::L('深化设计不能超过200个字符','__APPJIAJU_COMMON_LANG__@Model')),
				),
				'jiajucompany_deepbudget'=>array(
					array('max_length',200,Q::L('深化预算不能超过200个字符','__APPJIAJU_COMMON_LANG__@Model')),
				),
				'jiajucompany_materialquality'=>array(
					array('max_length',200,Q::L('材料质量不能超过200个字符','__APPJIAJU_COMMON_LANG__@Model')),
				),
				'jiajucompany_normativecontract'=>array(
					//array('max_length',200,Q::L('合同规范性不能超过200个字符','__APPJIAJU_COMMON_LANG__@Model')),
				),
				'jiajucompany_manage'=>array(
					array('max_length',200,Q::L('工程管理不能超过200个字符','__APPJIAJU_COMMON_LANG__@Model')),
				),
				'jiajucompany_specialservices'=>array(
					//array('max_length',200,Q::L('特色服务不能超过200个字符','__APPJIAJU_COMMON_LANG__@Model')),
				),
				'jiajucompany_announcement'=>array(
					array('max_length',200,Q::L('公司公告不能超过200个字符','__APPJIAJU_COMMON_LANG__@Model')),
				),
			),
		);
	}

	static function F(){
		$arrArgs=func_get_args();
		return ModelMeta::instance(__CLASS__)->findByArgs($arrArgs);
	}

	static function M(){
		return ModelMeta::instance(__CLASS__);
	}

	protected function beforeSave_(){
		$this->jiajucompany_fullname=C::text($this->jiajucompany_fullname);
		$this->jiajucompany_shortname=C::text($this->jiajucompany_shortname);
		$this->jiajucompany_address=C::text($this->jiajucompany_address);
		$this->jiajucompany_worktime=C::text($this->jiajucompany_worktime);
		$this->jiajucompany_qq=C::text($this->jiajucompany_qq);
		$this->jiajucompany_contactname=C::text($this->jiajucompany_contactname);
		$this->jiajucompany_telephone=C::cleanJs($this->jiajucompany_telephone);
		$this->jiajucompany_site=C::strip($this->jiajucompany_site);
		$this->jiajucompany_domain=C::text($this->jiajucompany_domain);
		$this->jiajucompany_email=C::strip($this->jiajucompany_email);
		$this->jiajucompany_fenji=C::text($this->jiajucompany_fenji);
		$this->jiajucompany_logo=C::text($this->jiajucompany_logo);
		$this->jiajucompany_baidumap=C::text($this->jiajucompany_baidumap);
		$this->jiajucompany_jiaotong=C::text($this->jiajucompany_jiaotong);
		$this->jiajucompany_description=C::text($this->jiajucompany_description);
		$this->jiajucompany_username=C::text($this->jiajucompany_username);
		$this->jiajucompany_editusername=C::text($this->jiajucompany_editusername);
		$this->jiajucompany_foundingyear=C::text($this->jiajucompany_foundingyear);
		$this->jiajucompany_scale=C::text($this->jiajucompany_scale);
		$this->jiajucompany_afterservices=C::text($this->jiajucompany_afterservices);
		$this->jiajucompany_earlydesign=C::text($this->jiajucompany_earlydesign);
		$this->jiajucompany_initialbudget=C::text($this->jiajucompany_initialbudget);
		$this->jiajucompany_designeddeep=C::text($this->jiajucompany_designeddeep);
		$this->jiajucompany_deepbudget=C::text($this->jiajucompany_deepbudget);
		$this->jiajucompany_materialquality=C::text($this->jiajucompany_materialquality);
		$this->jiajucompany_normativecontract=C::text($this->jiajucompany_normativecontract);
		$this->jiajucompany_manage=C::text($this->jiajucompany_manage);
		$this->jiajucompany_specialservices=C::text($this->jiajucompany_specialservices);
		$this->jiajucompany_announcement=C::text($this->jiajucompany_announcement);
	}

	public function getClass($nJiajucompanyId,$sType='jiajucompanybusinessclass'){
		$arrValue=Model::F_($sType,'jiajucompany_id=?',$nJiajucompanyId)->getColumn($sType=='jiajucompanydistrict'?'district_id':'jiajucompanyclass_id',true);
		if(!$arrValue){
			$arrValue=array();
		}
		return $arrValue;
	}

	public function getNum($sType,$arrWhere=array(),$arrStatus=array(0,1,2,3,11,12),$nCompanyId=null){
		$arrWhereDefault=array('jiajucompany_id'=>($nCompanyId===null?$GLOBALS['_company_']['jiajucompany_id']:$nCompanyId),'jiaju'.$sType.'_status'=>array('in',$arrStatus));
		if(!empty($arrWhere)){
			$arrWhereDefault=array_merge($arrWhereDefault,$arrWhere);
		}
		
		return Model::F_('jiaju'.$sType)
			->where($arrWhereDefault)
			->getCounts();
	}

	public function saveData(){
		// 启动事务
		$bRollback=false;
		$oDb=$this->getDb()->getConnect();
		$oDb->startTransaction();

		$this->site_id=Q::instance('DistrictModel')->getSiteid(Q::G('city_id','P'),Q::G('province_id','P'));
		$this->save();
		if($this->isError()){
			$bRollback=true;
		}

		// 保存商家介绍
		if($bRollback===false){
			$oJiajucompanycontent=new JiajucompanycontentModel();
			$oJiajucompanycontent->jiajucompany_id=$this->jiajucompany_id;
			$oJiajucompanycontent->save();
			if($oJiajucompanycontent->isError()){
				$bRollback=true;
				$this->_sErrorMessage=$oJiajucompanycontent->getErrorMessage();
			}
		}
		
		// 保存其他
		if($bRollback===false){
			$oJiajucompanymaster=new JiajucompanymasterModel();
			$oJiajucompanymaster->jiajucompany_id=$this->jiajucompany_id;
			$oJiajucompanymaster->save();
			if($oJiajucompanymaster->isError()){
				$bRollback=true;
				$this->_sErrorMessage=$oJiajucompanymaster->getErrorMessage();
			}
		}
		
		if($bRollback===false){
			$oJiajucompanyverify=new JiajucompanyverifyModel();
			$oJiajucompanyverify->jiajucompany_id=$this->jiajucompany_id;
			$oJiajucompanyverify->save();
			if($oJiajucompanyverify->isError()){
				$bRollback=true;
				$this->_sErrorMessage=$oJiajucompanyverify->getErrorMessage();
			}
		}
		
		if($bRollback===false){
			$oJiajucompanyvip=new JiajucompanyvipModel();
			$oJiajucompanyvip->jiajucompany_id=$this->jiajucompany_id;
			$oJiajucompanyvip->save();
			if($oJiajucompanyvip->isError()){
				$bRollback=true;
				$this->_sErrorMessage=$oJiajucompanyvip->getErrorMessage();
			}
		}

		$this->saveClass_();

		if($bRollback===false){
			$oDb->commit();
			return true;
		}else{
			$oDb->rollback();
			return false;
		}
	}

	public function updateData(){
		// 启动事务
		$bRollback=false;
		$oDb=$this->getDb()->getConnect();
		$oDb->startTransaction();

		$this->site_id=Q::instance('DistrictModel')->getSiteid(Q::G('city_id','P'),Q::G('province_id','P'));
		$this->save('update');
		if($this->isError()){
			$bRollback=true;
		}

		// 保存商家介绍
		if($bRollback===false){
			$oJiajucompanycontent=JiajucompanycontentModel::F('jiajucompany_id=?',$this->jiajucompany_id)->getOne();
			if(empty($oJiajucompanycontent['jiajucompany_id'])){
				$bRollback=true;
			}else{
				$oJiajucompanycontent->save('update');
				if($oJiajucompanycontent->isError()){
					$bRollback=true;
					$this->_sErrorMessage=$oJiajucompanycontent->getErrorMessage();
				}
			}
		}

		$this->saveClass_();

		if($bRollback===false){
			$oDb->commit();
			return true;
		}else{
			$oDb->rollback();
			return false;
		}
	}

	protected function saveClass_(){
		// 保存商家服务类型和服务区域
		$arrWhere=array('jiajucompany_id'=>$this->jiajucompany_id);
		Model::M_('jiajucompanyhomeclass')->deleteWhere($arrWhere);
		Model::M_('jiajucompanybusinessclass')->deleteWhere($arrWhere);
		Model::M_('jiajucompanystyleclass')->deleteWhere($arrWhere);
		Model::M_('jiajucompanydistrict')->deleteWhere($arrWhere);

		if(!empty($_POST['jiajucompanyhomeclass'])){
			foreach($_POST['jiajucompanyhomeclass'] as $nValue){
				Model::M_('jiajucompanyhomeclass')->insertWhere(
					array(
						'jiajucompany_id'=>$this->jiajucompany_id,
						'jiajucompanyclass_id'=>$nValue,
					)
				);
			}
		}

		if(!empty($_POST['jiajucompanybusinessclass'])){
			foreach($_POST['jiajucompanybusinessclass'] as $nValue){
				Model::M_('jiajucompanybusinessclass')->insertWhere(
					array(
						'jiajucompany_id'=>$this->jiajucompany_id,
						'jiajucompanyclass_id'=>$nValue,
					)
				);
			}
		}
		
		if(!empty($_POST['jiajucompanystyleclass'])){
			foreach($_POST['jiajucompanystyleclass'] as $nValue){
				Model::M_('jiajucompanystyleclass')->insertWhere(
					array(
						'jiajucompany_id'=>$this->jiajucompany_id,
						'jiajucompanyclass_id'=>$nValue,
					)
				);
			}
		}
		
		if(!empty($_POST['jiajucompanydistrict'])){
			foreach($_POST['jiajucompanydistrict'] as $nValue){
				Model::M_('jiajucompanydistrict')->insertWhere(
					array(
						'jiajucompany_id'=>$this->jiajucompany_id,
						'district_id'=>$nValue,
					)
				);
			}
		}
	}

	public function updateCount($nCompanyId){
		// 点评评分
		$oDb=Db::RUN();
		$arrData=array();
		$arrData=$oDb->getRow("SELECT SUM(jiajucompanycomment_level)/COUNT(1) AS ca,SUM(jiajucompanycomment_level1)/COUNT(1) AS c1,SUM(jiajucompanycomment_level2)/COUNT(1) AS c2,SUM(jiajucompanycomment_level3)/COUNT(1) AS c3,SUM(jiajucompanycomment_level4)/COUNT(1) AS c4,SUM(jiajucompanycomment_level5)/COUNT(1) AS c5,count(1) AS num FROM  `".Q::C('DB_PREFIX')."jiajucompanycomment` WHERE jiajucompany_id = {$nCompanyId} AND jiajucompanycomment_status=1 GROUP BY jiajucompany_id");

		if(!$arrData){
			$arrData=array('ca'=>3,'c1'=>'3.0','c2'=>'3.0','c3'=>'3.0','c4'=>'3.0','c5'=>'3.0','num'=>0);
		}
		foreach($arrData as $sK=>$v){
			if($sK=='ca'){
				$arrData[$sK]=round($v);
			}elseif($sK!='num'){
				$arrData[$sK]=round($v,1);
			}
		}
		
		Model::M_('jiajucompany')->updateWhere(
			array(
				'jiajucompany_casenum'=>$this->getNum('case',array(),array(1),$nCompanyId),
				'jiajucompany_roomnum'=>$this->getNum('case',array('jiajucase_ismodel'=>1),array(1),$nCompanyId),
				'jiajucompany_eventnum'=>$this->getNum('event',array(),array(1),$nCompanyId),
				'jiajucompany_diarynum'=>$this->getNum('diary',array(),array(1),$nCompanyId),
				'jiajucompany_anlinum'=>$this->getNum('anli',array(),array(1),$nCompanyId),
				'jiajucompany_packagenum'=>$this->getNum('package',array(),array(1),$nCompanyId),
				'jiajucompany_videonum'=>$this->getNum('video',array(),array(1),$nCompanyId),
				'jiajucompany_buildnum'=>$this->getNum('build',array(),array(1),$nCompanyId),
				'jiajucompany_designednum'=>$this->getNum('designer',array(),array(1),$nCompanyId),
				'jiajucompany_ordernum'=>$this->getNum('order',array(),array(1),$nCompanyId),
				'jiajucompany_commentnum'=>$this->getNum('companyguestbook',array(),array(1),$nCompanyId),
				'jiajucompany_guestbooknum'=>$this->getNum('companyguestbook',array(),array(1),$nCompanyId),
				'jiajucompanycomment_level'=>$arrData['ca'],
				'jiajucompanycomment_level1'=>$arrData['c1'],
				'jiajucompanycomment_level2'=>$arrData['c2'],
				'jiajucompanycomment_level3'=>$arrData['c3'],
				'jiajucompanycomment_level4'=>$arrData['c4'],
				'jiajucompanycomment_level5'=>$arrData['c5'],
			),
			'jiajucompany_id=?',$nCompanyId
		);
	}

}
